package com.homework.servlet;

import com.homework.entity.Cat;
import com.homework.service.CatService;
import com.homework.service.CatServiceImpl;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet({"/deleteCat","/updateCat","/insertCat","/selectCat","/selectList"})
public class CatServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        CatService catService = wac.getBean("catService", CatServiceImpl.class);

        String path = req.getServletPath();
        System.out.println(path);
        if (path.contains("insert")){
            String cname = req.getParameter("cname");
            String cage = req.getParameter("cage");
            String species = req.getParameter("species");
            String master = req.getParameter("master");
            System.out.println(cname+cage+species+master);
            Cat cat = new Cat();
            cat.setCname(cname);
            cat.setCage(Integer.parseInt(cage));
            cat.setSpecies(species);
            cat.setMaster(master);
            catService.insertCat(cat);
            req.getRequestDispatcher("/selectList").forward(req,resp);
        }else if (path.contains("delete")){
            String cno = req.getParameter("cno");
            catService.deleteCat(Integer.parseInt(cno));
            req.getRequestDispatcher("/selectList").forward(req,resp);
        }else if (path.contains("update")){
            System.out.println(path);
            String cno = req.getParameter("cno");
            String cname = req.getParameter("cname");
            String cage = req.getParameter("cage");
            String species = req.getParameter("species");
            String master = req.getParameter("master");
            System.out.println(cno+cname+cage+species+master);
            Cat cat = new Cat();
            cat.setCno(Integer.parseInt(cno));
            cat.setCname(cname);
            cat.setCage(Integer.parseInt(cage));
            cat.setSpecies(species);
            cat.setMaster(master);
            System.out.println(cat);
            catService.updateCat(cat);
            req.getRequestDispatcher("/selectList").forward(req,resp);
        }else if (path.contains("List")){
            List<Cat> list = catService.selectList();
            req.setAttribute("list",list);
            req.getRequestDispatcher("/cat.jsp").forward(req,resp);
        }else if (path.contains("selectCat")){
            System.out.println(path);
            String cno = req.getParameter("cno");
            String x = req.getParameter("x");
            System.out.println(x);
            System.out.println(cno);
            Cat cat = catService.selectCat(Integer.parseInt(cno));
            System.out.println(cat);
            req.setAttribute("cat",cat);
            req.getRequestDispatcher("/update.jsp").forward(req,resp);
        }
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

}
